Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
use my 1 round code to make a 3 rounds "Rock Paper Scissors" game - Javascript

I'm building a rock paper scissors game with Javascript, i did a 1 round mode of the game and it work, now i want to add a second mode " best of three rounds" but after trying many things i got my code messy and can't figure out what to do exactly.

i try to add a count = 0; count < 3; count = count + 1but don't know exactly where to put it

can someone help me please?

var mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
if (mode === '1') {
  oneRound;
}
if (mode === '2') {
  bestOfThree;
}

// set Computer Choice 

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if (computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
}

// Compare value for oneRound mode


var oneRound = function(computerChoice, userChoice) {
  if (computerChoice === userChoice) {
    return "The result is tie!";
  }
  if (computerChoice === "rock") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "paper")
        return "Player wins";
    }
  }
  if (computerChoice === "paper") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "rock")
        return "Player wins";
    }
  }
  if (computerChoice === "scissors") {
    if (userChoice === "rock") {
      return "Computer wins";
    } else {
      if (userChoice === "scissors")
        return "Player wins";
    }
  }

};

console.log("Player Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
console.log(oneRound(computerChoice, userChoice));

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Have look at this

I use setTimeout to allow the screen to show the result

const play = () => {
  // set Computer Choice 
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice <= 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }

  console.log("Player Choice: " + userChoice);
  console.log("Computer Choice: " + computerChoice);


  if (computerChoice === userChoice) {
    return "The result is tie!";
  }
  if (computerChoice === "rock") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "paper")
        return "Player wins";
    }
  }
  if (computerChoice === "paper") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "rock")
        return "Player wins";
    }
  }
  if (computerChoice === "scissors") {
    if (userChoice === "rock") {
      return "Computer wins";
    } else {
      if (userChoice === "scissors")
        return "Player wins";
    }
  }
};
const round = () => {
  const res = play();
  console.log(res)
  cnt--
  wins[cnt] = res.startsWith("Player") ? 1 : 0;
  if (cnt === 0) {
    const total = wins.reduce((a, b) => a + b)
    console.log(`You beat the computer ${total} time${total===1?"":"s"}`)
    return
  }
  setTimeout(round, 10) // else go again
}

let cnt = 1,
  wins = [];
const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
if (mode === '2') {
  cnt = 3;
}
round()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda